//右键菜单html
var menuHTML = 
		'<ul id="contextmenu" style="display:none;" class="contextmenu">' +
		'<li><a>顺时针旋转</a></li>' +
		'<li><a>逆时针旋转</a></li>' +
		'<li><a>更改颜色</a></li>' +
		'<li><a>放大</a></li>' +
		'<li><a>缩小</a></li>' +
		'<li><a>撤销上一次操作</a></li>' +
		'<li><a>删除该节点</a></li>' +
		'</ul>';

$(document).ready(function(){
	$("body").append(menuHTML); 
	
	var canvas = document.getElementById('canvas');//画布
	var stage = new JTopo.Stage(canvas);//舞台
	var scene = new JTopo.Scene(stage);//场景
	//显示工具栏 showJTopoToobar(stage);
	scene.background = './img/bg.jpg';//场景背景图片
	stage.add(scene);
	
	var node = new JTopo.Node("右键菜单-A");    
    node.setLocation(350,300);
    scene.add(node);
    
    var node2 = new JTopo.Node("右键菜单-B");    
    node2.setLocation(600,300);
    scene.add(node2);
    
    var currentNode = null;
    
    function handler(event){
    	if(event.button == 2){//右键
    		$("#contextmenu").css({
                top: event.pageY,
                left: event.pageX
            }).show();
    	}
    }
    
    node.addEventListener('mouseup', function(event){
        currentNode = this;
        handler(event);
    });
    
    node2.addEventListener('mouseup', function(event){
        currentNode = this;
        handler(event);
    });
    
    stage.click(function(event){
        if(event.button == 0){// 左键
            $("#contextmenu").hide();
        }
    });
    
	/* 右键菜单处理 */    
    $("#contextmenu a").click(function(){
        var text = $(this).text();
        
        if(text == '删除该节点'){
            scene.remove(currentNode);
            currentNode = null;
            $("#contextmenu").hide();
            return;
        }
        
        if(text == '撤销上一次操作'){
            currentNode.restore();
        }else{
            currentNode.save();
        }
        
        if(text == '更改颜色'){
            currentNode.fillColor = JTopo.util.randomColor();
        }else if(text == '顺时针旋转'){
            currentNode.rotate += 0.5;
        }else if(text == '逆时针旋转'){
            currentNode.rotate -= 0.5;
        }else if(text == '放大'){
            currentNode.scaleX += 0.2;
            currentNode.scaleY += 0.2;
        }else if(text == '缩小'){
            currentNode.scaleX -= 0.2;
            currentNode.scaleY -= 0.2;
        }
        
        $("#contextmenu").hide();
    });
    
    
});
	